/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ControllerBeans;

import Banks.*;
import Classes.DatabaseService;
import ModelBeans.BanksModelBean;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ActionEvent;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author xiph
 */
@ManagedBean
@RequestScoped
public class BanksControllerBean implements Serializable
{

	@ManagedProperty(value="#{banksModelBean}")
	private BanksModelBean banks;

	@ManagedProperty(value="#{fileUploadController}")
	private FileUploadController fuc;
	
	private String newName;

	public String getNewName() {
		return newName;
	}

	public void setNewName(String newName) {
		this.newName = newName;
	}
	
	public BanksModelBean getBanks() {
		return banks;
	}

	public FileUploadController getFuc() {
		return fuc;
	}

	public void setFuc(FileUploadController fuc) {
		this.fuc = fuc;
	}

	public void setBanks(BanksModelBean banks) {
		this.banks = banks;
	}
	
	public void addClick(ActionEvent e)
	{
		Bank b = new Bank();
		b.setName(getNewName());
		DatabaseService.addBank(b);
	}

	public BanksControllerBean()
	{
	}
	
	public void updateBank(RowEditEvent evt)
	{
		Bank b = (Bank) evt.getObject();
		if(!fuc.getEncodedFile().equals(""))
		{
			b.setIcon(fuc.getEncodedFile());
			fuc.setEncodedFile("");
		}
		DatabaseService.updateBank(b);
	}
	
	public void deleteBank(ActionEvent evt)
	{

		Bank b = (Bank) evt.getComponent().getAttributes().get("bank");
		DatabaseService.deleteBank(b);
	}
}
